
package data;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.upload.FormFile;
import java.util.*;

/**
 *
 * @author Alejandro
 */
public class IniciarBitacorasAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        IniciarBitacorasForm myForm = (IniciarBitacorasForm) form;

        // Informacion de la bitacora
        FormFile myFile = myForm.getArchivo();
        FormFile listasC = myForm.getListas();
        byte[] fileData = myFile.getFileData();
        String datos = new String(fileData);
        datos = datos.replaceAll("(\\r|\\n)", "");
        datos = datos.replaceAll(" ", "");
        String[] tokens = new String[100];
        
        StringTokenizer st = new StringTokenizer(datos,";");
        int index = 0;
        while (st.hasMoreTokens()) {
            tokens[index] = st.nextToken();
            index++;
        }
        IniciarBitacorasGateway gw = new IniciarBitacorasGateway();
        StringTokenizer stkn = new StringTokenizer(tokens[0],",");
        String anio = stkn.nextToken();
        String ciclo = stkn.nextToken();
        int i = 1;
        while(i<index){
            StringTokenizer stk = new StringTokenizer(tokens[i],",");
            String codBit = stk.nextToken();
            String sigCurso = stk.nextToken();
            String numGrupo = stk.nextToken();
            String cedProf = stk.nextToken();
            gw.crearProfesor(cedProf);
            gw.crearBitacora(codBit, sigCurso, numGrupo, cedProf, anio, ciclo);
            gw.crearCartaEstudiante(codBit, sigCurso);
            i++;
        }
        request.getSession().setAttribute("datos", datos);
        
        // Informacion de listas de clase
        byte[] listasData = listasC.getFileData();
        datos = new String(listasData);
        datos = datos.replaceAll("(\\r|\\n)", "");
        datos = datos.replaceAll(" ", "");
        tokens = new String[100];
        
        st = new StringTokenizer(datos,";");
        index = 0;
        while (st.hasMoreTokens()) {
            tokens[index] = st.nextToken();
            index++;
        }
        i = 1;
        while(i<index){
            StringTokenizer stk = new StringTokenizer(tokens[i],",");
            String codBit = stk.nextToken();
            String carnet = stk.nextToken();
            String nombre = stk.nextToken();
            String pApellido = stk.nextToken();
            String sApellido = stk.nextToken();
            gw.crearEstudianteLC(codBit,carnet,nombre,pApellido,sApellido);
            i++;
        }

        return mapping.findForward("success");
    }
}
